package domain;

import astar.AStar;
import domain.Game.action;
import domain.exceptions.CannotMoveThereException;
import java.util.LinkedList;
import java.util.Vector;

import miniMax.MiniMax;

/**
 *
 * @author shai
 */
public class AgentSmart extends Agent{

	private LinkedList<action> _actionsPath;

	public AgentSmart(String name, Game.action lastDirection, String assinment){
		super(name, lastDirection, assinment);
		this._actionsPath = new LinkedList<action>();
	}
//	public AgentSmart(String name, Game.action lastDirection, int arsenal, String assinment){
//		super(name, lastDirection, arsenal, assinment);
//		this._actionsPath = new LinkedList<action>();
//	}
	public AgentSmart(AgentSmart other) {
		super(other);
		this._actionsPath = other._actionsPath;
	}

	/*
	@Override
	public void die(Board board) {
		boolean whileThinking = board != Game._board;
		setHealth(Agent.DEAD);
		if (!whileThinking)
			updateGui(2);
	}
	*/
	
	@Override
	public void play(Board board, Vector<Agent> agents){
		if(this._actionsPath.isEmpty()){
			// This branch will happen after reaching a flag.
			System.out.println("Actions Path is Empty ---> Creating action path");
			if (getAssignment().equals("1")){
				thinkAStar();
			} else if (getAssignment().equals("2.1")){
				thinkMiniMax();
			}
			System.out.println("Smart Agent: I will go: "+_actionsPath);
		}
		Game.action act = this._actionsPath.poll();
		try {
			move(act, board, agents);
		} catch (CannotMoveThereException ex) {
			System.err.println(ex.getMessage());
		}
	}

	private void thinkMiniMax() {
		MiniMax miniMax = new MiniMax(this, Game.getOtherPlayer(this));
		this._actionsPath = miniMax.runMiniMaxSearch(Game._board, Game.getAgents(), Game._board.getFlags());
	}

	private void thinkAStar() {
		AStar aStar = new AStar(this);
		this._actionsPath = aStar.runAStarSearch(Game._board, Game.getAgents(), Game._board.getFlags());
	}
}